package com.controller.base;

import com.bean.base.TokenInfo;
import com.bean.base.UserInfo;
import com.bean.other.MetaInfo;
import com.bean.other.RequestData;
import com.bean.other.ResponseData;
import com.github.pagehelper.PageInfo;
import com.service.impl.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("api/user_info")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("getInfo")
    public ResponseEntity<ResponseData> getInfo(@RequestBody UserInfo userInfo){
        userInfo = userInfoService.get(userInfo.getCreator());
        TokenInfo tokenInfo=new TokenInfo();
        tokenInfo.setName(userInfo.getUsername());
        List<String> roles =new ArrayList<String>();
        //roles.add("admin");//需要设置角色， 角色与菜单关联
        if(userInfo.getUsername().equals("admin")){
            roles.add("admin");
        }else{
            roles.add("editor");
        }
        tokenInfo.setRoles(roles);
        tokenInfo.setUserid(userInfo.getId());
        return ResponseEntity.ok(ResponseData.success(tokenInfo));
    }

    @GetMapping("homePage")
    public ResponseEntity homePage(){

        return ResponseEntity.ok(null);
    }

    @PostMapping("add")
    public ResponseEntity<ResponseData> add(@RequestBody UserInfo userInfo){
        userInfoService.insert(userInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
        //return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("没有找到");//直接设置响应头
    }

    @PostMapping("edit")
    public ResponseEntity<ResponseData> edit(@RequestBody UserInfo userInfo){
        userInfoService.update(userInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @DeleteMapping("del/{id}")
    public ResponseEntity<ResponseData> del(@PathVariable String id){
        UserInfo userInfo=new UserInfo();
        userInfo.setId(id);
        userInfoService.delete(userInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @GetMapping("get")
    public ResponseEntity<ResponseData> get(@RequestParam("id")String id){
        UserInfo userInfo=userInfoService.get(id);
        return ResponseEntity.ok(ResponseData.success(userInfo));
    }

    @PostMapping("list")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> list(@RequestBody RequestData<UserInfo> requestData){
        UserInfo userInfo=requestData.getObj();
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<UserInfo> pageInfo=userInfoService.select(userInfo,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }


    @PostMapping("init")
    public ResponseEntity<ResponseData> init(@RequestBody UserInfo userInfo){
        //新增的时候，用户名 不能重复
        String message = userInfoService.insert(userInfo);
        if(message.contains("成功")){
            return ResponseEntity.ok(ResponseData.success(message));
        }else{
            return ResponseEntity.ok(ResponseData.error(message));
        }
        //return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("没有找到");//直接设置响应头
    }

}
